<?php

class Wordpress extends Controller {

	var $bloguser = "ttsgs";
	var $blogpass = "ttsgs007";
	//var $xmlrpc_server = "http://wp.cms.net/xmlrpc.php";
	var $xmlrpc_server = "http://wp.ttsgs.com/xmlrpc.php";
	  
	function Wordpress()
	{
		parent::Controller();	
		$this->load->library("dailynews_lib");
		//$this->output->enable_profiler(true);		
	}
	
	function index()
	{
		exit;
	}
	
	function sync()
	{
		set_time_limit(10*60);
		$this->load->model("dailynews_model");
		$nlist = $this->dailynews_model->getList(array(),"newsdate desc",1,4);
		$posts = $this->getRecentPostsByDb();
		if(count($posts)<3) return ;
		foreach($nlist as $row)
		{
			$title = $this->dailynews_lib->filtertitle($row->title);
			$body = $this->dailynews_lib->filterbody($row->body);
			
			if($row->newsdate>time())  
				$row->newsdate = time()-3600*(8)-60;
			$date = date("Y-m-d H:i:s",$row->newsdate);
			$is_exist = false;
			foreach($posts as $post)
			{
				if(trim($title) == trim($post["title"]))
				{
					$is_exist = true;
				}	
			}
			if(!$is_exist)
			{
				
				$this->newPost($title,$body,$date);
			}			


		}
		echo "OK";
	}
	
	
	function getRecentPostsByDb()
	{
		$wp = $this->load->database("wp",TRUE);
		$query = $wp->query('select post_title title from wp_posts order by id desc limit 20');
		$ret = array();
		foreach ($query->result_array() as $row)
		{
		   $ret[] = $row;
		}		
		return $ret;
	}
	 
	function getRecentPosts()
	{
	   $this->load->library('xmlrpc');
	   $this->load->helper('date');
	   $blogid = 1;
	   $this->xmlrpc->server($this->xmlrpc_server, 80);
	   //$this->xmlrpc->server("http://wp.ttsgs.com/xmlrpc.php", 80);
	   $this->xmlrpc->method('metaWeblog.getRecentPosts');
	   $request = array($blogid, $this->bloguser, $this->blogpass);
	   $this->xmlrpc->request($request);
	   $result = $this->xmlrpc->send_request();
	   if ( !$result )
	   {
	       echo $this->xmlrpc->display_error();
	       return array();
	   }
	   else
	   {
	       $ret = $this->xmlrpc->display_response();
	       return $ret;
	   }
	}
		
	public function test()
	{
		$this->newPost("t","b","2011-5-24");
	}

	public function newPost($title,$body,$date){

	   $this->load->library('xmlrpc');
	   $this->load->helper('date');
	   $blogid = 1;

	   $publishImmediately = "true";

	   $this->xmlrpc->set_debug(FALSE);
	   $this->xmlrpc->server($this->xmlrpc_server, 80);
	   //$this->xmlrpc->server("http://wp.ttsgs.com/xmlrpc.php", 80);
	   $this->xmlrpc->method('metaWeblog.newPost');
  	
		$fdate = date("Ymd\TH:i:s\Z",strtotime($date));
		
		      	
      	$categories = array("news");
      	//'categories' => array($categories,'struct'),
      	
      	
		$post=array(array(
		'dateCreated' =>array($fdate,'dateTime.iso8601'),
		'description' =>array( $body,'string'),		
		'categories' => array($categories,'struct'),
		'title'  =>array( $title,'string')),'struct'
		);	
	   $request = array($blogid, $this->bloguser, $this->blogpass, $post, array($publishImmediately,'boolean'));
	
	   $this->xmlrpc->request($request);
	   $result = $this->xmlrpc->send_request();
	
	   if ( !$result )
	   {
	       echo $this->xmlrpc->display_error();
	   }
	   else
	   {
	       return $this->xmlrpc->display_response();
	   }
	} 	
	
	
}
